<?php
require_once("dao/IDao.php");
require_once("model/Hole.php");
require_once("model/Course.php");

class HoleDAO implements IDao{
	var $conn;
	
	public function __construct($conn){
		$this->conn =& $conn;
	}
	public function getItem($holeid){
		return self::getList($holeid, null,null,null)[0];
	}
	
	public function getList($holeid,$courseid,$coursename,$holename){
		$result = null;
		try{
			if($stmt = $this->conn->prepare("CALL HOLE_READ_LIST(?,?,?,?,?)")){
				$deleted = 0;
				$stmt->bind_param("iissi",$holeid,$courseid,$coursename,$holename,$deleted);
				$stmt->execute();
			
				$stmt->bind_result($_holeid,$_holename,$_holeorder,$_par,$_courseid,$_coursename,$_deleted);
			
				$result = array();
			
				while($stmt->fetch()){
					$hole = new Hole();
					$hole->holeid = $_holeid;
					$hole->course = Course::initWithParam($_courseid, $_coursename,null,null,null,null,null,null,null,null,null);
					$hole->holeorder = $_holeorder;
					$hole->par = $_par;
					$hole->holename = $_holename;
					array_push($result,$hole);
				}
				$stmt->close();
			}
			else{
				printf("Error occurred in %s.<br/>Details: %s",__CLASS__,$stmt->error);
			}
		}
		catch(Exception $e){
			throw new Exception($e->getMessage());
		}
		
		return $result;
	}
	public function saveItem($hole){
		try{
			if(isset($hole->holeid)){
				if($stmt = $this->conn->prepare("CALL HOLE_UPDATE(?,?,?,?,?,?)")){
					$stmt->bind_param("iiisii",
							$hole->holeid,
							$hole->course->courseid,
							$hole->holeorder,
							$hole->holename,
							$hole->par,
							$hole->deleted);
						
					$stmt->execute();
				
					if($stmt->error != ""){
						printf("Error %s",$stmt->error);
						exit();
					}
				}
			}
			else{
				
			}
		}
		catch (Exception $e){
			throw new Exception($e->getMessage());
		}
	}
	public function deleteItem($id){}
	public function __destruct(){}
}
?>